package ssm.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ssm.dao.PersonBase;
import ssm.dao.Teacher;
import ssm.dao.Yhzpb;
import ssm.dao.YktCarinfo;
import ssm.model.PersonMapper;
import ssm.model.TeacherMapper;
import ssm.model.YhzpbMapper;
import ssm.model.YktCarinfoMapper;

import java.util.List;
import java.util.Map;

@Service
public class PersonServiceImpl implements PersonService {

    @Autowired
    private PersonMapper personMapper;
    @Autowired
    private YhzpbMapper yhzpbMapper;
    @Autowired
    private YktCarinfoMapper yktCarinfoMapper;
    @Autowired
    private TeacherMapper teacherMapper;




    public Object find(String name) {

        String xm = name.replaceAll("[0-9]", "");
        String jid = name.replaceAll(xm, "");
        System.out.println("xm:" + xm);
        System.out.println("jid:" + jid);
        System.out.println(jid.equals(""));
        if (!xm.equals("") && !jid.equals("")) {
            System.out.println("执行查询jid跟xm");
            PersonBase result = personMapper.findByXmAndJid(jid, xm);
            if (result != null) {
                Yhzpb yhzpb = yhzpbMapper.findByJid(result.getJid());
                if (yhzpb != null) {
                    result.setZp(yhzpb.getZp());
                }
            }
            return result;
        }
        if (!(jid.equals(""))) {
            System.out.println("执行查询jid");
            PersonBase result = personMapper.findByJid(jid);
            if (result != null) {
                Yhzpb yhzpb = yhzpbMapper.findByJid(result.getJid());
                if (yhzpb != null) {
                    result.setZp(yhzpb.getZp());
                }
            }
            return result;
        }
        if (!(xm.equals(""))) {
            System.out.println("执行查询xm");
            List<PersonBase> result = personMapper.findByXm(xm);
            for (PersonBase per : result) {
                Yhzpb yhzpb = yhzpbMapper.findByJid(per.getJid());
                if (yhzpb != null) {
                    per.setZp(yhzpb.getZp());
                }
            }
            return result;
        }

        return null;

    }

    public Teacher SearchTeacherByCph(String cph) {
        if(cph==null&&"".equals(cph)){
            return null;
        }
        String gh= yktCarinfoMapper.selectJidhByCph(cph);
        if(gh==null&&"".equals(gh)){
            return null;
        }
        Teacher teacher=teacherMapper.getByGh(gh);
        Yhzpb yhzpb = yhzpbMapper.findByJid(gh);
        if (yhzpb != null) {
            byte[] zp = yhzpb.getZp();
            System.out.println(zp);
            teacher.setZp(zp);
        }

        return teacher;
    }
}
